package com.NewtifyMe;



import java.util.ArrayList;
import java.util.HashMap;




import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;

public class NewtifyMe extends ListActivity {
	   private MyOnItemSelectedListener selMin = new MyOnItemSelectedListener();
	   private EditText text;
	   private SearchList searchList = new SearchList();
	   
	   private PopupWindow pw;
	   
	   private boolean keywordView = true;
	   private String CurrentKeywordView = null;
	   
	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		// Create an array of Strings, that will be put to our ListActivity
		//String[] names = new String[] {"Linux", "Windows7", "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone"};
		
		searchList.addKeyword("puppy");
		searchList.addKeyword("corvette");
		searchList.addKeyword("android");
		
		searchList.getSearchList().get("puppy").addUrl("Puppy vs. Cat", "http://www.youtube.com/watch?v=7bcV-TL9mho");
		
		refresh(null);
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String clickedItem = o.toString();
		
		if (keywordView){
			CurrentKeywordView = clickedItem;
			
			keywordView = false;
			refresh(clickedItem);
		}else{
			
			String url = searchList.getSearchList().get(CurrentKeywordView).getUrl(clickedItem);
			
			Intent intent = new Intent(Intent.ACTION_CHOOSER, Uri.parse(url));
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			startActivity(intent);
		}
		
		
//		Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
		
//	    String[] names = new String[] {"Linux","Linux","Linux", "Windows7", "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone"};
		
//	    this.setListAdapter(new MyArrayAdapter(this, names));
		
//		setContentView(R.layout.add_keyword);		
		

		
	}
	public void popUpAdd(){
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
	    View pview = inflater.inflate(R.layout.add_keyword, null, false);
		
		pw = new PopupWindow(pview,  480,  480, true); 
	    pw.showAtLocation(this.findViewById(R.id.label), Gravity.TOP, 0, 20);  
	    
	    
	    Spinner spinner = (Spinner) pview.findViewById(R.id.rep_time);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.planets_array, android.R.layout.simple_spinner_item);
	    
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    
	    spinner.setOnItemSelectedListener(selMin);
	  
	    
	    text = (EditText)pview.findViewById(R.id.editText1);
	    
	    Button button = (Button)pview.findViewById(R.id.button1);
	    button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				int min = selMin.getMin();
				String keyword = text.getText().toString();
				
				if(keyword.length() == 0){
					Toast.makeText(v.getContext(), "Type Something Please", Toast.LENGTH_LONG).show();
					return;
				}
				
				
				//TODO: send url request using keyword and min
				String str = "uid=1&init&" + keyword + "&" + min;
				HTTPRequestPoster hp = new HTTPRequestPoster();
				hp.sendGetRequest("http://192.168.1.2:8080/init", str);
				
				searchList.addKeyword(text.getText().toString());
				pw.dismiss();
				refresh(null);
				
				
			}
		});
	}
	public void refresh(String keyword){
		if (keywordView){
			this.setListAdapter(new MyArrayAdapter(this,searchList.getKeywordList() ));
			//keywordView = false;
		}else{
			this.setListAdapter(new MyArrayAdapter(this,searchList.getSearchList().get(keyword).getTitleList()));
			//keywordView = true;
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.add_menu, menu);
	    return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.add_keyword:
	    	popUpAdd();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}